﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using QLNhaThuoc.DTO;

namespace QLNhaThuoc.DAL
{
    class CTPhieuDA:DataProvider
    {
        public string LoaiMaPhieu { get; set; }

        public void insert(CTPhieu info)
        {
            DataRow row = myTable.NewRow();
            row[LoaiMaPhieu] = info.Ma;
            row["MaThuoc"] = info.MaThuoc;
            row["SoLuong"] = info.SoLuong;
            row["ThanhTien"] = info.ThanhTien;
            myTable.Rows.Add(row);

            updateMyTable();
        }

        public void delete(CTPhieu info)
        {
            myTable.Rows.Find(new object[] { info.Ma, info.MaThuoc }).Delete();

            updateMyTable();
        }

        public void update(CTPhieu info)
        {
            DataRow row = myTable.Rows.Find(new object[] { info.Ma, info.MaThuoc });
            row[LoaiMaPhieu] = info.Ma;
            row["MaThuoc"] = info.MaThuoc;
            row["SoLuong"] = info.SoLuong;
            row["ThanhTien"] = info.ThanhTien;

            updateMyTable();
        }

        public bool KiemTraPK(CTPhieu info)
        {
            DataRow row = myTable.Rows.Find(new object[] { info.Ma, info.MaThuoc });
            if (row != null)
                return true;
            return false;
        }

        public CTPhieu[] LayListCTPhieu(int ma)
        {
            ArrayList list = DongHopDuLieuTuCustomDatatable("select * from " + TableName + " where " + LoaiMaPhieu + "=" + ma);
            int n = list.Count;
            CTPhieu[] kq = new CTPhieu[n];

            for (int i = 0; i < n; i++)
            {
                kq[i] = (CTPhieu)list[i];
            }

            return kq;
        }

        public override object ConvertOneRowIntoDTO(DataTable table, int i)
        {
            CTPhieu obj = new CTPhieu();
            obj.Ma = int.Parse(table.Rows[i][LoaiMaPhieu].ToString());
            obj.MaThuoc = table.Rows[i]["MaThuoc"].ToString();
            obj.SoLuong = int.Parse(table.Rows[i]["SoLuong"].ToString());
            obj.ThanhTien = int.Parse(table.Rows[i]["ThanhTien"].ToString());

            return obj;
        }
    }
}
